package com.cd.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.cd.bean.Contact;
import com.cd.bean.Cooperation;
import com.cd.bean.Dictionary;
import com.cd.bean.Guide;
import com.cd.bean.News;
import com.cd.bean.Party;
import com.cd.bean.Product;
import com.cd.bean.Property;
import com.cd.bean.Science;
import com.cd.bean.SearchCondition;
import com.cd.bean.Technology;
import com.cd.service.contact.ContactService;
import com.cd.service.cooperation.CooperationService;
import com.cd.service.dictionary.DictionaryService;
import com.cd.service.guide.GuideService;
import com.cd.service.news.NewsService;
import com.cd.service.party.PartyService;
import com.cd.service.product.ProductService;
import com.cd.service.property.PropertyService;
import com.cd.service.science.ScienceService;
import com.cd.service.search.SearchService;
import com.cd.service.technology.TechnologyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 网站入口
 *@Author zhk 1003589522
 *@Date 2018-1-4
 **/
@RequestMapping("/portal")
@Controller
public class PortalController {

	@Autowired
	NewsService newsService;
	@Autowired
	ScienceService scienceService;
	@Autowired
	TechnologyService technologyService;
	@Autowired 
	CooperationService cooperationService;
	@Autowired
	ProductService productService;
	@Autowired 
	DictionaryService dictionaryService;
	@Autowired
	PartyService partyService;
	@Autowired
	ContactService contactService;
	@Autowired 
	SearchService searchService;
	@Autowired
	PropertyService propertyService;
	@Autowired
	GuideService guideService;
	
	/**
	 * 网站入口，进入主界面
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/go")
	public ModelAndView goIndex() throws Exception {
		ModelAndView mv = new ModelAndView();
		List<News> newslist = newsService.listLimit();
		List<Science> scilist = scienceService.listLimit();
		List<Technology> teclist = technologyService.listLimit();
		List<Cooperation> coolist = cooperationService.listLimit();
		List<Dictionary> diclist = dictionaryService.listLimit();
		List<Product> prolist = productService.listLimit();
		List<Party> partylist = partyService.listLimit();
		List<Contact> conlist = contactService.listLimit();
		List<Property> propertylist = propertyService.listLimit();
		List<Guide> guidelist = guideService.listLimit();
		mv.addObject("guidelist", guidelist);
		mv.addObject("partylist", partylist);
		mv.addObject("conlist", conlist);
		mv.addObject("newslist", newslist);
		mv.addObject("scilist", scilist);
		mv.addObject("teclist", teclist);
		mv.addObject("coolist", coolist);
		mv.addObject("diclist", diclist);
		mv.addObject("prolist", prolist);
		mv.addObject("propertylist", propertylist);
		mv.setViewName("frame/main");
		return mv;
	}
	/**
	 * 搜索功能
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/search")
	public ModelAndView search(@RequestParam(value = "pn",defaultValue="1")Integer pn,@RequestParam(value="keyword")String keyword) throws Exception {
		ModelAndView mv = new ModelAndView();
		System.out.println("控制层："+keyword);
		SearchCondition sc = new SearchCondition();
		sc.setKeyword(keyword);
		PageHelper.startPage(pn, 15);//开始分页
		List<SearchCondition> list = searchService.search(sc);
		if(list.isEmpty()){
			mv.addObject("keyword", keyword);
			mv.setViewName("search_error");
		}else{
			PageInfo page = new PageInfo(list,5);//设置分页信息
			mv.addObject("pageInfo", page);
			mv.setViewName("search_list");
		}
		return mv;
	}
	
	
}
